package com.aaa.service.impl;

import com.aaa.dao.RoleDao;
import com.aaa.entity.Menu;
import com.aaa.entity.PageSort;
import com.aaa.entity.Role;
import com.aaa.service.RoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleDao roleDao;

    /**
     * 通过ID查询单条数据
     */
    @Override
    public Role queryById(Integer roleId) {
        return this.roleDao.queryById(roleId);
    }

    @Override
    public Page findAllRole(PageSort pageSort) {
        //开始分页
        PageHelper.startPage(pageSort.getPage(), pageSort.getLimit());
        List<Role> roleList = roleDao.findAllRole();
        com.github.pagehelper.Page listWithPage = (com.github.pagehelper.Page) roleList;
        return listWithPage;
    }

    /**
     * 查询多条数据
     */

    /**
     * 新增数据
     */
    @Override
    public Role insert(Role role) {
        this.roleDao.insert(role);
        return role;
    }

    /**
     * 修改数据
     */
    @Override
    public Role update(Role role) {
        this.roleDao.update(role);
        return this.queryById(role.getRoleId());
    }

    /**
     * 通过主键删除数据
     */
    @Override
    public boolean deleteById(Integer roleId) {

    roleDao.deleteByRoleId(roleId);
    return this.roleDao.deleteById(roleId)>0;
    }


    /**
     * @description: 获取数据条数
     */
    @Override
    public long getCount() {
        return roleDao.getCount();
    }


    /**
     * @description: 根据角色ID删除

     */
    @Override
    public int deleteByRoleId(Integer roleId) {
        return roleDao.deleteByRoleId(roleId);
    }

//    查找菜单
    @Override
    public List<Menu> findAllMenu() {
        return roleDao.findAllMenu();
    }
//添加角色菜单
    @Override
    public void insertRoleMenu(int roleId, String[] menusId) {
        for(int i=0;i<menusId.length;i++){
            roleDao.insertRoleMenu(roleId,menusId[i]);
        }

    }

    /**
     * 修改角色彩色
     */
    @Override
    public void updateRoleMenu(Integer roleId, String[] menusId) {
        roleDao.deleteRoleMenu(roleId);
        for (int i=0;i<menusId.length;i++){
            roleDao.insertRoleMenu(roleId,menusId[i]);
        }
    }

    /**
     * 查询角色菜单
     */
    @Override
    public List<Integer> findSelectMenu(int roleId) {
        return roleDao.findSelectMenu(roleId);
    }


}
